package photobox.utils

import groovyx.net.http.HTTPBuilder

import static groovyx.net.http.ContentType.TEXT
import static groovyx.net.http.ContentType.XML
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.GET

class SendSms {
    static def sendUri = 'http://www.smsbao.com/sms?'
    def channelName = "duanXinBao"
    def mobiles = []
    def userName = "99tucang"
    def password = '((tucang'.encodeAsMD5()
    //http://www.smsbao.com/sms?u=USERNAME&p=PASSWORD&m=PHONE&c=CONTENT
    //用户名99tucang        密码((tucang

    def addMobiles(phones) {
        if (!phones instanceof List) {
            phones = [phones]
        }
        mobiles += phones
    }

    def sendMsg(content, encode="UTF-8") {
        def  result = [code: null, message: "请求失败", balance: -1, failMap: [:]]
        if (mobiles.size() == 0) return result
        //mobiles = ["1860112x550","18601129550", "1234"]
        def mobilesStr = mobiles.join(",")
        String msg = content
        if (encode) {
            msg = URLEncoder.encode(content, encode)
        }
        def url = sendUri + "&u=${userName}&p=${password}&m=${mobilesStr}&c=${msg}"
        def http = new HTTPBuilder(url)

        http.request(GET, TEXT) { req ->
            response.success = { resp, reader ->
                assert resp.status == 200
                println(reader.text)
                //写入数据库
            }

            // called only for a 404 (not found) status code:
            response.'404' = { resp ->
                log.error "sendMsg 404: ${url}"
                result = [code: -1, message: "请求失败", balance: -1, failMap: [:]]
                mobiles.each {
                    result.failMap[it] = [reason: "-1", desc: "请求为404"]
                }
            }
        }
        return result
    }

    def parseErr(code) {
        def errors = [
                "0": "成功",
                "-1100": "验证失败 账号或者密码不争取",
                "-1101": "账号不存在",
                "-1102": "密码错误",
                "-1103": "错误的输入参数",
                "-1104": "账号被暂停",
                "-1105": "账号没有使用接口的权限",
                "-1106": "当前IP不允许访问",

                "-1201": "发送 号码参数错误",
                "-1202": "发送 内容参数错误",
                "-1203": "发送 扩展号码错误",
                "-1204": "发送 扩展参数错误",
                "-1205": "发送 内容包含敏感词",
                "-1206": "发送 费用不足",
                "-1207": "发送 号码被屏蔽",
                "-1208": "发送 不是有效的号码",
                "-1209": "发送 该号码重复出现",
                "-1210": "发送 短信类型无效",

                "-1301": "接收 位置参数错误",
                "-1302": "接收 与上一次接收时间间隔过短 请稍后再接收",
                "-1303": "接收 服务器忙 请稍后再试",

                "-1501": "设置属性 错误的属性名称",
                "-1502": "设置属性 错误的属性值",
                "-1503": "设置属性 不支持操作",
                "-1504": "设置密码失败。请确定原密码和账号正确",

                "-1601": "获得账号属性时错误，系统错误",
                "-1701": "扩展功能接口错误。不支持的操作",
                "-1000": "短信接口服务内部错误，系统错误",
                "-1011": "获得账号信息时错误，系统错误"
        ]
        code = "${code}".trim()
        if (errors.containsKey(code)) {
            return errors[code]
        } else {
            return "未知错误"
        }
    }
}